home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 5 / CRON_1 / CRON_SET / CRON / ARGC_REC.C < prev    next >
Text File  |  1991-09-22  |  7KB  |  320 lines

  1. /*    ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  2.     
  3.     Filename: "argc Receiver.c"
  4.     argc/argv argument receiver code, written for THINK C 5.0
  5.     By Chris Johnson
  6.     Version of: Sunday, September 15, 1991 7:00 PM
  7.     
  8.     Distribute freely and without charge, but say something nice about 
  9.     the author when you use it.  Please send me a copy of any improve-
  10.     ments you make so they can be incorporated into future versions.
  11.     
  12.     ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  13.     Internet:    chrisj@emx.utexas.edu
  14.     UUCP:        {husc6|uunet}!cs.utexas.edu!ut-emx!chrisj
  15.     BitNet:        chrisj@utxvm.bitnet
  16.     AppleLink:    chrisj@emx.utexas.edu@internet#
  17.     CompuServe:    >INTERNET:chrisj@emx.utexas.edu
  18.     US Mail:    Chris Johnson, 3311 Red River #305, Austin, TX 78705
  19.     ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤    */
  20.  
  21.  
  22. #include "argc Receiver.h"
  23. #include <AppleEvents.h>
  24. #include <GestaltEqu.h>
  25.  
  26.  
  27. #define argcEventClass        'args'
  28. #define argcEventID            'argc'
  29. #define argcKeyword            'argc'
  30. #define LBitMask(BitNum)    (0x1L << (BitNum))
  31.  
  32. #define _WaitNextEvent        0xA860
  33. #define _Gestalt            0xA1AD
  34.  
  35.  
  36. static Boolean            WaitFlag;
  37. static int                argc;
  38. static unsigned char    **argv;
  39.  
  40.  
  41. Boolean                    EnvironmentCheck(void);
  42.  
  43. pascal OSErr            Quit(AppleEvent *RcvdEvent, AppleEvent *ReplyEvent, long RefCon);
  44. pascal OSErr            ArgsGet(AppleEvent *RcvdEvent, AppleEvent *ReplyEvent, long RefCon);
  45. void                    ArgsDispose(void);
  46. OSErr                    MyGotRequiredParams(AppleEvent *Event);
  47. void                    HandleHighLevelEvent(EventRecord *Event);
  48.  
  49. int                        NumToolboxTraps(void);
  50. TrapType                GetTrapType(int theTrap);
  51. Boolean                    TrapAvailable(int theTrap);
  52.  
  53.  
  54.  
  55.  
  56. void argcReceiver(argcRcvd, argvRcvd)
  57. int                        *argcRcvd;
  58. char                    ***argvRcvd;
  59. {
  60.     ArgsDispose();
  61.     
  62.     if (EnvironmentCheck()) {
  63.         unsigned long            TimeoutTicks;
  64.         
  65.         TimeoutTicks = Ticks + 60 * 60;
  66.         
  67.         while (WaitFlag) {
  68.             EventRecord                Event;
  69.             
  70.             if (WaitNextEvent(everyEvent, &Event, 60, NULL)) {
  71.                 
  72.                 switch (Event.what) {
  73.                         
  74.                     case keyDown:
  75.                         WaitFlag = FALSE;
  76.                         break;
  77.                         
  78.                     case kHighLevelEvent:
  79.                         AEProcessAppleEvent(&Event);
  80.                         break;
  81.                 }
  82.             }
  83.             
  84.         //    If we're still waiting and we've been waiting more than 
  85.         //    a minute so far it's time to quit.
  86.             
  87.             if (Ticks >= TimeoutTicks)
  88.                 WaitFlag = FALSE;
  89.         }
  90.         
  91.         AERemoveEventHandler(kCoreEventClass, kAEQuitApplication, Quit, FALSE);
  92.         AERemoveEventHandler(argcEventClass, argcEventID, ArgsGet, FALSE);
  93.     }
  94.     
  95.     if (argv == NULL)
  96.         ExitToShell();
  97.     
  98.     *argcRcvd = argc;
  99.     *(unsigned char ***) argvRcvd = argv;
  100. }
  101.  
  102.  
  103. static Boolean EnvironmentCheck() {
  104.     Boolean                    Continue;
  105.     
  106.     Continue = FALSE;
  107.     
  108.     if (TrapAvailable(_WaitNextEvent)) {
  109.         
  110.         if (TrapAvailable(_Gestalt)) {
  111.             long                    Response;
  112.             
  113.             if (Gestalt(gestaltAppleEventsAttr, &Response) == noErr) {
  114.                 
  115.                 if ((Response & LBitMask(gestaltAppleEventsPresent)) != 0) {
  116.                     
  117.                     if (AEInstallEventHandler(kCoreEventClass, kAEQuitApplication, Quit, 0, FALSE) == noErr) {
  118.                         
  119.                         if (AEInstallEventHandler(argcEventClass, argcEventID, ArgsGet, 0, FALSE) == noErr) {
  120.                             
  121.                             WaitFlag = TRUE;
  122.                             Continue = TRUE;
  123.                         }
  124.                     }
  125.                 }
  126.             }
  127.         }
  128.     }
  129.     
  130.     return (Continue);
  131. }
  132.  
  133.  
  134. static pascal OSErr Quit(RcvdEvent, ReplyEvent, RefCon)
  135. AppleEvent                *RcvdEvent;
  136. AppleEvent                *ReplyEvent;
  137. long                    RefCon;
  138. {
  139.     OSErr                    OSError;
  140.     
  141.     OSError = MyGotRequiredParams(RcvdEvent);
  142.     if (OSError == noErr)
  143.         WaitFlag = FALSE;
  144.     
  145.     return (OSError);
  146. }
  147.  
  148.  
  149. static pascal OSErr ArgsGet(RcvdEvent, ReplyEvent, RefCon)
  150. AppleEvent                *RcvdEvent;
  151. AppleEvent                *ReplyEvent;
  152. long                    RefCon;
  153. {
  154.     OSErr                    OSError;
  155.     AEDesc                    ArgCList;
  156.     
  157.     OSError = AEGetParamDesc(RcvdEvent, argcKeyword, typeAEList, &ArgCList);
  158.     if (OSError == noErr) {
  159.         long                    ArgCount;
  160.         
  161.         OSError = AECountItems(&ArgCList, &ArgCount);
  162.         if (OSError == noErr) {
  163.             
  164.             argc = ArgCount;
  165.             argv = (unsigned char **) NewPtrClear(sizeof(Ptr) * (ArgCount + 1));
  166.                     
  167.             OSError = MemError();
  168.             if (OSError == noErr) {
  169.                 long                    Index;
  170.                 unsigned char            **CurArgV;
  171.                 
  172.                 Index = 0;
  173.                 CurArgV = argv;
  174.                 
  175.                 while (--ArgCount >= 0 && OSError == noErr) {
  176.                     DescType                DataType;
  177.                     long                    DataSize;
  178.                     
  179.                     OSError = AESizeOfNthItem(&ArgCList, ++Index, &DataType, &DataSize);
  180.                     if (OSError == noErr) {
  181.                         
  182.                         *CurArgV = (unsigned char *) NewPtr(DataSize + 1);
  183.                         OSError = MemError();
  184.                         if (OSError == noErr) {
  185.                             AEKeyword                Keyword;
  186.                             long                    ActualSize;
  187.                             
  188.                             OSError = AEGetNthPtr(&ArgCList, Index, typeChar, &Keyword, &DataType, (Ptr) *CurArgV, DataSize, &ActualSize);
  189.                             if (OSError == noErr)
  190.                                 *(*CurArgV + DataSize) = '\0';
  191.                         }
  192.                     }
  193.                     
  194.                     CurArgV++;
  195.                 }
  196.                 
  197.             }
  198.         }
  199.         
  200.         AEDisposeDesc(&ArgCList);
  201.     }
  202.     
  203.     if (OSError == noErr)
  204.         OSError = MyGotRequiredParams(RcvdEvent);
  205.     
  206.     if (OSError != noErr) {
  207.         
  208.         argc = 0;
  209.         argv = NULL;
  210.     }
  211.     
  212.     WaitFlag = FALSE;
  213.     
  214.     return (OSError);
  215. }
  216.  
  217.  
  218. static OSErr MyGotRequiredParams(Event)
  219. AppleEvent                *Event;
  220. {
  221.     OSErr                    OSError;
  222.     DescType                ReturnedType;
  223.     Size                    ActualSize;
  224.     
  225.     OSError = AEGetAttributePtr(Event, keyMissedKeywordAttr, typeWildCard, &ReturnedType, NULL, 0, &ActualSize);
  226.     if (OSError == errAEDescNotFound) {
  227.         
  228.         OSError = noErr;
  229.         
  230.     } else if (OSError == noErr)
  231.         OSError = errAEEventNotHandled;
  232.     
  233.     return (OSError);
  234. }
  235.  
  236.  
  237. static void ArgsDispose() {
  238.     
  239.     if (argv != NULL) {
  240.         unsigned char            **CurArg;
  241.         
  242.         CurArg = argv;
  243.         
  244.         while (--argc >= 0) {
  245.             
  246.             if (*CurArg != NULL)
  247.                 DisposPtr((Ptr) *CurArg);
  248.                 
  249.             CurArg++;
  250.         }
  251.         
  252.         DisposPtr((Ptr) argv);
  253.     }
  254.     
  255.     argc = 0;
  256.     argv = NULL;
  257. }
  258.  
  259.  
  260. /*    ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  261.     All of the following code comes from IM VI pg. 3-8.
  262.     ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤    */
  263.  
  264.  
  265. #define _Unimplemented    0xA89F
  266. #define _InitGraf        0xA86E
  267.  
  268.  
  269. int                        NumToolboxTraps(void);
  270. TrapType                GetTrapType(int theTrap);
  271. Boolean                    TrapAvailable(int theTrap);
  272.  
  273.  
  274.  
  275.  
  276. static int NumToolboxTraps() {
  277.     int                        NumToolboxTraps;
  278.     
  279.     if (NGetTrapAddress(_InitGraf, ToolTrap) == NGetTrapAddress(0xAA6E, ToolTrap))
  280.         NumToolboxTraps = 0x200;
  281.     else
  282.         NumToolboxTraps = 0x400;
  283.         
  284.     return (NumToolboxTraps);
  285. }
  286.  
  287.  
  288. static TrapType GetTrapType(theTrap)
  289. int                        theTrap;
  290. {
  291.     TrapType                tType;
  292.     
  293.     if ((theTrap & 0x0800) > 0)
  294.         tType = ToolTrap;
  295.     else
  296.         tType = OSTrap;
  297.     
  298.     return (tType);
  299. }
  300.  
  301.  
  302. Boolean TrapAvailable(theTrap)
  303. int                        theTrap;
  304. {
  305.     TrapType                tType;
  306.     
  307.     tType = GetTrapType(theTrap);
  308.     
  309.     if (tType == ToolTrap) {
  310.         
  311.         theTrap &= 0x07FF;
  312.         if (theTrap >= NumToolboxTraps())
  313.             theTrap = _Unimplemented;
  314.     }
  315.     
  316.     return (NGetTrapAddress(theTrap, tType) != NGetTrapAddress(_Unimplemented, ToolTrap));
  317. }
  318.  
  319.  
  320.